package com.deyuanyun.pic.pbf.app.aop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.deyuanyun.pic.pbf.domain.RepairTask;
import com.deyuanyun.pic.pbf.enumeration.RepairStateEnum;
import com.deyuanyun.pic.pbf.mapper.RepairTaskMapper;
import com.deyuanyun.pic.settings.aop.BasePushMsgAspect;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.PushMessage;
import com.deyuanyun.pic.settings.service.msgtemplate.pbf.RepairProjectPushMsg;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jerry 维修维护app项目完成切入点
 * @Description
 * @date 2016-12-05
 */
@Service
@Aspect
public class AppRepairTaskAspect extends BasePushMsgAspect {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());


    @Resource
    private RepairTaskMapper repairTaskMapper;//维修任务mapper

    /**
     * 维修项目切入点
     *
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-10-09
     */
    @Pointcut(value = "@annotation(pushMessage) && target(com.deyuanyun.pic.pbf.app.service.PbfService)")
    private void repairAppTaskServicePointcut(PushMessage pushMessage) {
    }

    /**
     * 在{@link com.deyuanyun.pic.pbf.app.service.PbfService#postForm(String)}方法执行之后织入，进行消息推送
     *
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-10-09
     */
    @AfterReturning(value = "repairAppTaskServicePointcut(pushMessage) && execution(* postForm(..))")
    public void updateAppRepairTaskStatedAdvice(JoinPoint joinPoint, PushMessage pushMessage) {
        try {

            SpelExpressionParser parser = new SpelExpressionParser();
            EvaluationContext context = super.getEvaluationContext(joinPoint);
            String[] values = pushMessage.value();
            if (values.length > 0) {
                String param = parser.parseExpression(values[0]).getValue(context, String.class);
                JSONObject obj = JSON.parseObject(param);
                String projectId = obj.getString("ProjectID");
                RepairTask repairTask = repairTaskMapper.selectByPrimaryKey(projectId);
                if (repairTask != null && RepairStateEnum.ALREADY_REPAIR.toString().equals(repairTask.getRepairState())) {//当是已维修的时候，为项目关闭
                    LOGGER.info("单个维修任务点击已维修,得到的维修任务id为：" + projectId);
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put(RepairProjectPushMsg.REPAIR_IDS, new String[]{projectId});
                    map.put(RepairProjectPushMsg.REPAIR_FLAG, "已关闭");
                    pushMsg(map);
                }
            }
        } catch (Exception e) {
            LOGGER.error("维修项目消息推送异常", e);
        }
    }

    /**
     * 推送消息
     *
     * @param map
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-10-09
     */
    private void pushMsg(Map<String, Object> map) {
        //当map不为空的时候调用接口推送消息
        if (map != null && !map.isEmpty()) {
            LOGGER.info("开始推送消息");
            AbstractPushMsgTemplate repairProjectPushMsg = new RepairProjectPushMsg();
            repairProjectPushMsg.setInterface(false, map);
            repairProjectPushMsg.pushMsg();
        }
    }
}
